package Greedy;

public class _738_MonotoneIncreasingDigits {
    public int monotoneIncreasingDigits(int N) {
        String number = String.valueOf(N);
        String results = "";
        search:for(int i=0;i<number.length();i++) {
            for(char c = '1';c<='9';c++) {
                if (number.compareTo(results + repeat(c, number.length() - i))<0) {
                    results += (char)(c - 1);
                    continue search;
                }
            }
            results += '9';
        }
        return Integer.parseInt(results);
    }
    public String repeat(char c,int count) {
        StringBuffer sb = new StringBuffer(count);
        for(int i=0;i<count;i++) {
            sb.append(c);
        }
        return sb.toString();
    }
}
